package leetcode.code2155;

import java.util.ArrayList;
import java.util.List;

import leetcode.IDebug;
import leetcode.helper.H;

public class Solution implements IDebug {

	public List<Integer> maxScoreIndices1(int[] nums) {
		int len = nums.length;
		int[] sums = new int[len + 1];
		for (int i = 1; i < sums.length; i++) {
			sums[i] = sums[i - 1] + nums[i - 1];
		}
		int max = -1;
		List<Integer> ans = null;
		for (int i = 0; i <= len; i++) {
			int numLeft = i - sums[i];
			int numRight = sums[len] - sums[i];
			int score = numLeft + numRight;
			if (score > max) {
				ans = new ArrayList<>();
				ans.add(i);
				max = score;
			} else if (score == max) {
				ans.add(i);
			}
		}
		return ans;
	}

	public List<Integer> maxScoreIndices(int[] nums) {
		int len = nums.length;
		int[] sums = this.presum(nums);
		int max = -1;
		List<Integer> ans = null;
		for (int i = 0; i <= len; i++) {
			int numLeft = i - sums[i];
			int numRight = this.sumIndex(sums, i, len - 1);
			int score = numLeft + numRight;
			if (score > max) {
				ans = new ArrayList<>();
				ans.add(i);
				max = score;
			} else if (score == max) {
				ans.add(i);
			}
		}
		return ans;
	}

	public int[] presum(int[] nums) {
		int len = nums.length;
		int[] sums = new int[len + 1];
		for (int i = 1; i < sums.length; i++) {
			sums[i] = sums[i - 1] + nums[i - 1];
		}
		return sums;
	}

	// [indexL,indexR]
	public int sumIndex(int[] sums, int indexL, int indexR) {
		return sums[indexR + 1] - sums[indexL];
	}

	public int sumNo(int[] sums, int noL, int noR) {
		return sums[noR] - sums[noL];
	}

	@Override
	public void debug4() {
		H.compare(H.str2array3("[2,4]"), this.maxScoreIndices(H.str2array3("[0,0,1,0]")));

	}

	@Override
	public void debug3() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug2() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug1() {
		// TODO Auto-generated method stub

	}

	public static void main(String[] args) {
		Solution so = new Solution();
		so.debug1();
		so.debug2();
		so.debug3();
		so.debug4();

	}

}
